package com.station.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 配置动态数据源 注解
 * @Author: dongfangyu
 * @Date: 2025/9/18
 */

@Configuration
public class DynamicDataSourceConfig {

    @Autowired
    @Qualifier("masterDataSource")
    private DataSource masterDataSource;

    @Autowired
    @Qualifier("baseDataSource")
    private DataSource baseDataSource;

    @Autowired
    @Qualifier("financeDataSource")
    private DataSource financeDataSource;

    @Autowired
    @Qualifier("orderDataSource")
    private DataSource orderDataSource;

    @Autowired
    @Qualifier("erpDataSource")
    private DataSource erpDataSource;


    @Bean
    @Primary
    public DynamicDataSource dataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("master", masterDataSource);
        dataSourceMap.put("base", baseDataSource);
        dataSourceMap.put("finance", financeDataSource);
        dataSourceMap.put("order", orderDataSource);
        dataSourceMap.put("erp", erpDataSource);

        dynamicDataSource.setTargetDataSources(dataSourceMap);
        dynamicDataSource.setDefaultTargetDataSource(masterDataSource);
        return dynamicDataSource;
    }
}
